package com.joshua.spring.configuration;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicate;
import com.joshua.spring.property.SwaggerGroupInfoConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.RestController;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * description
 * package com.joshua.spring.gateway.configuration
 * classname  SwaggerConfiguration
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/6/6 16:32
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
    @Autowired
    private SwaggerGroupInfoConfig groupInfoConfig;

    @Bean(value = "dragonOauth")
    @Order(value = 1)
    public Docket groupRestApi() {
        Predicate<RequestHandler> predicate = input -> {
            Class<?> declaringClass = input.declaringClass();
            if(declaringClass.isAnnotationPresent(RestController.class)) {
                return true;
            }
            return false;
        };
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupApiInfo())
                .select()
                .apis(predicate)
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo groupApiInfo() {
        Contact contact = new Contact(groupInfoConfig.getContactName(),
                groupInfoConfig.getContactUrl(), groupInfoConfig.getContactEmail());
        return new ApiInfoBuilder()
                .title(groupInfoConfig.getTitle())
                .description("<div style='font-size:14px;color:red;'>" + groupInfoConfig.getDescription() + "</div>")
                .termsOfServiceUrl(groupInfoConfig.getTermsOfServiceUrl())
                .contact(contact)
                .version(groupInfoConfig.getVersion())
                .build();
    }
}
